#include "../source/server.hpp"
// 联调Sock模块
// int main()
// {
//     Socket lst_sock;
//     lst_sock.CreateServer(8500);
//     while(1)
//     {
//         int newfd = lst_sock.Accept();
//         if(newfd < 0)
//         {
//             continue;
//         }
//         Socket cli_sock(newfd);
//         char buf[1024] = {0};
//         int ret = cli_sock.Recv(buf, 1023);
//         if(ret < 0)
//         {
//             cli_sock.Close();
//             continue;
//         }
//         cli_sock.Send(buf, ret);
//         cli_sock.Close();
//     }
//     lst_sock.Close();
//     return 0;
// }

// poller和channel模块的联调
// void HandleClose(Channel* channel)
// {
//     std::cout << "close fd: " << channel->Fd() << std::endl;
//     LOG_DBG("close fd:%d", channel->Fd());
//     channel->Remove(); //移除监控
//     delete channel;
// }
// void HandleRead(Channel* channel)
// {
//     int fd = channel->Fd();
//     char buffer[1024] = {0};
//     int n = recv(fd, buffer, 1023, 0);
//     if(n <= 0)
//     {
//         return HandleClose(channel);//关闭释放
//     }
//     LOG_DBG("%s", buffer);
//     channel->EnableWrite(); //启动可写事件
// }
// void HandleWrite(Channel* channel)
// {   
//     int fd = channel->Fd();
//     const char* data = "我叫吕培！！！";
//     int n = send(fd, data, strlen(data), 0);
//     if(n < 0)
//     {
//         return HandleClose(channel);//关闭释放
//     }
//     channel->DisableWrite(); //关闭可写监控
// }
// void HandleError(Channel* channel)
// {
//     return HandleClose(channel);//关闭释放
// }
// void HandleEvent(EventLoop* loop,Channel* channel, uint64_t timerid)
// {
//     loop->TimerRefresh(timerid);
// }







std::unordered_map<uint64_t, PtrConnection> _conns;
uint64_t conn_id = 0; 
EventLoop base_loop;
LoopThreadPool *loop_pool;

// 管理所有的连接
void ConnectionDestroy(const PtrConnection& conn)
{
    _conns.erase(conn->Id());
}
void OnConnected(const PtrConnection& conn)
{
    LOG_DBG("New Connection:%p", conn.get());
}
void OnMessage(const PtrConnection& conn, Buffer* buf)
{
    LOG_DBG("%s", buf->ReadPostion());
    buf->MoveReadOffset(buf->ReadableDataSize());
    std::string str = "我是吕培";
    conn->Send(str.c_str(), str.size());
    // conn->Shutdown();  
}

void NewConnection(int fd)
{
    PtrConnection connection(new Connection(loop_pool->NextLoop(), conn_id, fd));
    connection->SetMessageCallback(std::bind(OnMessage, std::placeholders::_1, std::placeholders::_2));  
    connection->SetCloseCallback(std::bind(ConnectionDestroy, std::placeholders::_1)); 
    connection->SetConnectionCallback(std::bind(OnConnected, std::placeholders::_1)); 
    // connection->SetAnyEventCallback(std::bind(HandleEvent, connection)); 
    connection->EableInactiveRelease(10); //启动非活跃超时销毁
    connection->Established(); //就绪初始化---启动读事件监控
    _conns.insert(std::make_pair(conn_id, connection));
    LOG_DBG("New ---------");
    
}
int main()
{
    loop_pool = new LoopThreadPool(&base_loop);
    loop_pool->SetThreadCount(2);
    loop_pool->Create();
    Acceptor acceptor(&base_loop, 8500);
    acceptor.SetAcceptorCallBack(std::bind(NewConnection, std::placeholders::_1)); //std::placeholders::_1站位参数
    acceptor.Listen(); //进行读事件监听
    base_loop.Start();
    return 0;
}

